package com.yangguang.huahui.ui.adapters;

import android.content.Context;
import android.view.View;
import android.widget.ListView;

import com.yangguang.huahui.R;
import com.yangguang.huahui.entity.FlowersType;
import com.yangguang.huahui.listadpter.CommonAdapter;
import com.yangguang.huahui.listadpter.ViewHolder;

import java.util.List;

/**
 * Created by Administrator on 2017/3/22.
 */

public class PinzhongAdapter extends CommonAdapter<FlowersType.ListBean>{
    private OnItemClickListener mOnItemClickListener;
    private static ListView lv_second;
    private static ListView lv_third;
    private static int firstPosition;
    private static int secondPostion;
    private static int thirdPosition;


    public PinzhongAdapter(Context context, List<FlowersType.ListBean> data, int layoutId,  ListView lv_second,
                           ListView lv_third, int firstPosition, int secondPostion, int thirdPosition) {
        super(context, data, layoutId);
        this.lv_second = lv_second;
        this.lv_third = lv_third;
        this.firstPosition = firstPosition;
        this.secondPostion = secondPostion;
        this.thirdPosition = thirdPosition;
    }

    @Override
    public void convert(final ViewHolder holder, final FlowersType.ListBean listBean) {
        holder.setText(R.id.tv_content, listBean.getClassname());
        if (firstPosition > -1 && firstPosition == holder.getPosition()){
            holder.setBackgroundResource(R.id.tv_content, R.mipmap.choose_item_selected);
            if (listBean.getSubclass().size() > 0) {
                lv_second.setAdapter(new SecondAdapter(mContext, listBean.getSubclass(), R.layout.select_area_list_item));
                lv_second.setVisibility(View.VISIBLE);
            } else {
                lv_second.setVisibility(View.GONE);
            }
        } else {
            holder.setBackgroundResource(R.id.tv_content, R.color.white);
        }
        holder.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                notifyDataSetChanged();
                firstPosition = holder.getPosition();
                secondPostion = -1;
                thirdPosition = -1;
                if (listBean.getSubclass().size() > 0) {
                    lv_second.setAdapter(new SecondAdapter(mContext, listBean.getSubclass(), R.layout.select_area_list_item));
                    lv_second.setVisibility(View.VISIBLE);
                } else {
                    lv_second.setVisibility(View.GONE);
                }
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(view, holder.getPosition(),
                            listBean.getId(), listBean.getClassname());
                }
            }
        });
    }
    public static class SecondAdapter extends CommonAdapter<FlowersType.ListBean.SubclassBean>{
        private static OnItemClickListener mOnItemClickListener;

        public SecondAdapter(Context context, List<FlowersType.ListBean.SubclassBean> data, int layoutId) {
            super(context, data, layoutId);
        }

        @Override
        public void convert(final ViewHolder holder, final FlowersType.ListBean.SubclassBean subclassBean) {
            holder.setText(R.id.tv_content, subclassBean.getClassname());
            if (secondPostion > -1 && secondPostion == holder.getPosition()){
                holder.setBackgroundResource(R.id.tv_content, R.mipmap.choose_item_selected);
                if (subclassBean.getSunclass().size() > 0) {
                    lv_third.setAdapter(new ThirdAdapter(mContext, subclassBean.getSunclass(), R.layout.select_area_list_item));
                    lv_third.setVisibility(View.VISIBLE);
                } else {
                    lv_third.setVisibility(View.GONE);
                }
            } else {
                holder.setBackgroundResource(R.id.tv_content, R.color.white);
            }
            holder.getConvertView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    notifyDataSetChanged();
                    secondPostion = holder.getPosition();
                    thirdPosition = -1;
                    if (subclassBean.getSunclass().size() > 0) {
                        lv_third.setAdapter(new ThirdAdapter(mContext, subclassBean.getSunclass(), R.layout.select_area_list_item));
                        lv_third.setVisibility(View.VISIBLE);
                    } else {
                        lv_third.setVisibility(View.GONE);
                    }
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(view, holder.getPosition(),
                                subclassBean.getId(), subclassBean.getClassname());
                    }
                }
            });
        }
        public static class ThirdAdapter extends CommonAdapter<FlowersType.ListBean.SubclassBean.SunclassBean>{
            private static OnItemClickListener mOnItemClickListener;
            public ThirdAdapter(Context context, List<FlowersType.ListBean.SubclassBean.SunclassBean> data, int layoutId) {
                super(context, data, layoutId);
            }

            @Override
            public void convert(final ViewHolder holder, final FlowersType.ListBean.SubclassBean.SunclassBean sunclassBean) {
                holder.setText(R.id.tv_content, sunclassBean.getClassname());
                if (thirdPosition > -1 && thirdPosition == holder.getPosition()){
                    holder.setBackgroundResource(R.id.tv_content, R.mipmap.choose_item_selected);
                } else {
                    holder.setBackgroundResource(R.id.tv_content, R.color.white);
                }
                holder.getConvertView().setOnClickListener( new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        notifyDataSetChanged();
                        thirdPosition = holder.getPosition();
                        if (mOnItemClickListener != null) {
                            mOnItemClickListener.onItemClick(view, holder.getPosition(),
                                    sunclassBean.getId(), sunclassBean.getClassname());
                        }
                    }
                });
            }

            public static void setOnItemClickListener(OnItemClickListener l) {
                mOnItemClickListener = l;
            }
        }

        public static void setOnItemClickListener(OnItemClickListener l) {
            mOnItemClickListener = l;
        }
    }

    public void setOnItemClickListener(OnItemClickListener l) {
        mOnItemClickListener = l;
    }
}
